{extend name="base" /}

{block name="title"}角色管理{/block}

{block name="body"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 数据表格 -->
            <table id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="roleTbBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="auth">权限分配</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleEditDialog">
    <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色名称</label>
            <div class="layui-input-block">
                <input name="title" placeholder="请输入角色名称" class="layui-input" autocomplete="off" lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色描述</label>
            <div class="layui-input-block">
                <textarea name="comments" placeholder="请输入角色描述" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="roleEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" mx-event="closeDialog">取消</button>
        </div>
    </form>
</script>
{/block}

{block name="script"}
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'zTree'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#roleTable',
            url: "{:url('mxadmin/role/datalist')}",
            page: {limit: 15},
            limits: [15, 30, 45, 60, 75, 90, 100, 200, 500],
            toolbar: ['<p>',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>批量删除</button>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加角色</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'id', title: '角色ID', align: 'center', sort: true, width: 90},
                {field: 'title', title: '角色名称', align: 'center', sort: true},
                {field: 'comments', title: '角色描述', align: 'center',},
                {field: 'create_time', title: '创建时间', align: 'center', sort: true},
                {field: 'update_time', title: '更新时间', align: 'center', sort: true},
                {title: '操作', toolbar: '#roleTbBar', align: 'center', minWidth: 200}
            ]],
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
        });

        /* 表格工具条点击事件 */
        table.on('tool(roleTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'auth') {  // 权限管理
                showPermModel(obj.data.id, obj.data.title);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(roleTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('roleTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 5, anim: 6});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '角色',
                shade: 0.5,
                //shadeClose: true,
                content: $('#roleEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('roleEditForm', mData);
                    // 表单提交事件
                    form.on('submit(roleEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? "{:url('mxadmin/role/edit')}" : "{:url('mxadmin/role/add')}", data.field, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();  // 保存成功刷新表格
                            } else {
                                layer.msg(res.msg, {icon: 5, anim: 6});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中的数据吗？', {
                title: '删除数据',
                skin: 'layui-layer-admin',
                shade: 0.5,
                shadeClose: true,
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post("{:url('mxadmin/role/del')}", {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 1) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.msg, {icon: 5, anim: 6});
                    }
                }, 'json');
            });
        }

        /* 权限管理 */
        function showPermModel(id, title) {
            admin.open({
                title: '"'+title+'"'+'的角色权限分配',
                shade: 0.5,
                //shadeClose: true,
                btn: ['保存', '取消'],
                content: '<ul id="roleAuthTree" class="ztree"></ul>',
                success: function (layero, dIndex) {
                    var loadIndex = layer.load(2);
                    $.post("{:url('mxadmin/role/authlist')}", {id: id}, function (res) {
                        layer.close(loadIndex);
                        if (res.code === 1) {
                            var insTree = $.fn.zTree.init($('#roleAuthTree'), {
                                check: {enable: true},
                                data: {key: {name: "title"}, simpleData: {enable: true, pIdKey: "pid"}},
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        $.fn.zTree.getZTreeObj("roleAuthTree").checkNode(treeNode, !treeNode.checked, true);
                                    }
                                }
                            }, res.data);
                            // 默认展开全部节点
                            insTree.expandAll(true);
                        } else {
                            layer.msg(res.msg, {icon: 5, anim: 6});
                        }
                    }, 'json');
                    // 超出一定高度滚动
                    $(layero).children('.layui-layer-content').css({'max-height': '300px', 'overflow': 'auto'});
                },
                yes: function (dIndex) {
                    var insTree = $.fn.zTree.getZTreeObj('roleAuthTree');
                    var checkedRows = insTree.getCheckedNodes(true);
                    var ids = [];
                    for (var i = 0; i < checkedRows.length; i++) {
                        ids.push(checkedRows[i].id);
                    }
                    var loadIndex = layer.load(2);
                    $.post("{:url('mxadmin/role/authlist_edit_same')}", {id: id, rules: ids.join(',')}, function (res) {
                        layer.close(loadIndex);
                        if (res.code === 1) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(dIndex);
                        } else {
                            layer.msg(res.msg, {icon: 5, anim: 6});
                        }
                    }, 'json');
                }
            });
        }
    });
</script>
{/block}
